package com.lehecai.core.lottery.fetcher.jcgyj.impl.worker;

import com.lehecai.core.exception.FetchFailedException;
import com.lehecai.core.lottery.JcgyjRaceStatus;
import com.lehecai.core.lottery.fetcher.FetcherType;
import com.lehecai.core.lottery.fetcher.jcgyj.AbstractJcgyjScheduleFetchWorker;
import com.lehecai.core.lottery.fetcher.jcgyj.IJcgyjScheduleFetcher;
import com.lehecai.core.lottery.fetcher.jcgyj.JcgyjScheduleItem;
import com.lehecai.core.lottery.fetcher.jcgyj.impl.CommonJcgyjScheduleFetcher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用竞彩足球猜冠亚军赛程抓取实现类
 * @author sunshow
 *
 */
public class CommonJcgyjScheduleFetchWorkerOfficial extends AbstractJcgyjScheduleFetchWorker {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public List<JcgyjScheduleItem> fetch(String phase)
            throws FetchFailedException {

        if (StringUtils.isEmpty(phase)) {
            throw new FetchFailedException("必须指定彩期");
        }

        JSONArray raceArray;
        try {
            raceArray = AbstractJcgyjFetchWorkerOfficial.fetchAll(phase);
        } catch (Exception e) {
            throw new FetchFailedException("抓取竞彩猜冠军数据出错, phase=" + phase);
        }

        if (raceArray == null || raceArray.isEmpty()) {
            logger.warn("抓取竞彩猜冠军数据为空, phase={}", phase);
            return null;
        }

        List<JcgyjScheduleItem> jcgyjScheduleItemList = new ArrayList<JcgyjScheduleItem>();
        for (Object aRaceArray : raceArray) {
            JSONObject raceObject = (JSONObject) aRaceArray;

            JcgyjScheduleItem item = new JcgyjScheduleItem();
            item.setPhase(phase);
            item.setMatchNum(raceObject.getString("matchNum"));
            item.setOfficialNum(raceObject.getString("officialNum"));
            item.setHomeTeam(raceObject.getString("homeTeam"));
            item.setAwayTeam(raceObject.getString("awayTeam"));
            item.setStatus(JcgyjRaceStatus.getItem(raceObject.getInt("status")));

            jcgyjScheduleItemList.add(item);
        }

        return jcgyjScheduleItemList;
    }
    public static void main(String[] args) throws Exception {
        IJcgyjScheduleFetcher fetcher = new CommonJcgyjScheduleFetcher();
        List<JcgyjScheduleItem> jcgyjScheduleItemList = fetcher.fetch("2014001", FetcherType.T_OFFICIAL);
        for (JcgyjScheduleItem item : jcgyjScheduleItemList) {
            System.out.println(item.getJczqScheduleInfo());
        }
    }
}
